/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package action.product;
import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import database.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.Product;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Product")
public class ManagerProductAction extends ActionSupport{
   
    private Integer newQuantity;
    private Integer idProduct;
    private String newName;
    private Boolean isRemove;
    private String button;
    private List<Product> products;
    private ProductManagerDAO productDAO = new ProductManagerDAO();
    private InventoryDAO inventoryDAO = new InventoryDAO();
    private int currentPage;
    private int totalPage;
    private int itemsPerPage = 10; 
    private String keyword;
    private Map<Integer,String> sorts;
    private Integer sort;

    public Map<Integer, String> getSorts() {
        return sorts;
    }

    public void setSorts(Map<Integer, String> sorts) {
        this.sorts = sorts;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public  ManagerProductAction()
    {
        sorts = new HashMap<Integer, String>();
        sorts.put(1, "Product Code");
        sorts.put(2, "Product Name");
        sorts.put(3, "Unit Price");
        sorts.put(4, "Stock Quantity");
    }
    public ProductManagerDAO getProductDAO() {
        return productDAO;
    }

    public void setProductDAO(ProductManagerDAO productDAO) {
        this.productDAO = productDAO;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }
    

    /**
     * @return the newQuantity
     */
    public Integer getNewQuantity() {
        return newQuantity;
    }

    /**
     * @param newQuantity the newQuantity to set
     */
    public void setNewQuantity(Integer newQuantity) {
        this.newQuantity = newQuantity;
    }

    /**
     * @return the newName
     */
    public String getNewName() {
        return newName;
    }

    /**
     * @param newName the newName to set
     */
    public void setNewName(String newName) {
        this.newName = newName;
    }

    /**
     * @return the isRemove
     */
    public Boolean getIsRemove() {
        return isRemove;
    }

    /**
     * @param isRemove the isRemove to set
     */
    public void setIsRemove(Boolean isRemove) {
        this.isRemove = isRemove;
    }

    /**
     * @return the button
     */
    public String getButton() {
        return button;
    }

    /**
     * @param button the button to set
     */
    public void setButton(String button) {
        this.button = button;
    }
    @Action(value= "product-management", results = {
	@Result(name ="success",location="/manageProduct/w_product.jsp")
    })
    public String view()
    {    
        Map session = ActionContext.getContext().getSession();
                session.put("product", 1);
        products = inventoryDAO.getProductForInventory(1);
        int[] idProduct = new int[products.size()];
        int i = 0;
        for(Product item: products)
        {
            idProduct[i] = item.getIdProduct();
            i++;
        }
        session.put("idProduct", idProduct);
        return SUCCESS;
    }
     @Action(value = "add-product", results = {
        @Result(name = "success", location = "/manageProduct/w_addproduct.jsp")
         
    })
    public String AddProductView() throws Exception {

        return SUCCESS;
    }
    @Action(value="delete-product/*", 
            results={
        @Result(name="success", location = "/manageProduct/w_product.jsp")},
            params = {"idProduct", "{1}"})
    public String deleteProduct()
    {
        System.out.println(idProduct);
        if(productDAO.DeleteProductByProductID(idProduct));
        {
            List<Product> temp;
        products = productDAO.GetAllProduct();
        return SUCCESS;
        }
        
    }
    @Action(value = "sort-product", results = {
        @Result(name = "success", location = "/manageProduct/w_product.jsp")
    })
    public String Sort(){
         Map session = ActionContext.getContext().getSession();
                session.put("product", 3);
        products = new InventoryDAO().getProductForInventory(sort);
        int[] idProduct = new int[products.size()];
        int i = 0;
        for(Product item: products)
        {
            idProduct[i] = item.getIdProduct();
            i++;
        }
        session.put("idProduct", idProduct);
        
        return SUCCESS;
    }
    @Action(value = "searchProduct", results = {
        @Result(name = "success", location = "/manageProduct/w_product.jsp"),
        @Result(name = "error", location = "/manageProduct/w_product.jsp")
    })
    public String searchProduct() {
         Map session = ActionContext.getContext().getSession();
                
        if (keyword == null || keyword.length() < 3) {
            addActionError("Keyword must be greater than 2.");
            products = new InventoryDAO().getProductForInventory(1);
            session.put("product", 1);
            int[] idProduct = new int[products.size()];
            int i = 0;
            for(Product item: products)
            {
                idProduct[i] = item.getIdProduct();
                i++;
            }
        session.put("idProduct", idProduct);
            return ERROR;
        } else {
            products = new ProductDAO().searchForProductsByKeyword(keyword);
            session.put("product", 2);
            int[] idProduct = new int[products.size()];
            int i = 0;
            for(Product item: products)
            {
                idProduct[i] = item.getIdProduct();
                i++;
            }
            session.put("idProduct", idProduct);
            return SUCCESS;
        }
        
    }
    public List<Product> getProducts() {
        return products;
    }

    /**
     * @param products the products to set
     */
    public void setProducts(List<Product> products) {
        this.products = products;
    }

    /**
     * @return the currentPage
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * @param currentPage the currentPage to set
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * @return the totalPage
     */
    public int getTotalPage() {
        return totalPage;
    }

    /**
     * @param totalPage the totalPage to set
     */
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * @return the itemsPerPage
     */
    public int getItemsPerPage() {
        return itemsPerPage;
    }

    /**
     * @param itemsPerPage the itemsPerPage to set
     */
    public void setItemsPerPage(int itemsPerPage) {
        this.itemsPerPage = itemsPerPage;
    }

    /**
     * @return the idProduct
     */
    public Integer getIdProduct() {
        return idProduct;
    }

    /**
     * @param idProduct the idProduct to set
     */
    public void setIdProduct(Integer idProduct) {
        this.idProduct = idProduct;
    }

    /**
     * @return the hiddenProductID
     */

    /**
     * @return the idProduct
     */
    
}

